package com.heima.admin.service.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdUserMapper;
import com.heima.admin.service.AdUserService;
import com.heima.apis.user.IUserClient;
import com.heima.apis.wemedia.IWeMediaClient;
import com.heima.common.constants.AduserConstants;
import com.heima.model.admin.dtos.AdUserDto;
import com.heima.model.admin.dtos.AuthDto;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.common.dtos.PageRequestDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.utils.common.AppJwtUtil;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 管理员用户信息表 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
@Transactional
public class AdUserServiceImpl extends ServiceImpl<AdUserMapper, AdUser> implements AdUserService {
@Autowired
private IUserClient userClient;
@Autowired
private IWeMediaClient weMediaClient;
    @Override
    public ResponseResult login(AdUserDto dto) {
        if (!StringUtils.isEmpty(dto.getName()) && !StringUtils.isEmpty(dto.getPassword())){
            AdUser adUser = getOne(Wrappers.<AdUser>lambdaQuery().eq(AdUser::getName, dto.getName()));
            if (adUser == null){
                return ResponseResult.errorResult(AppHttpCodeEnum.AD_USER_DATA_NOT_EXIST);
            }
            String salt = adUser.getSalt();
            String newPassword = DigestUtils.md5DigestAsHex((dto.getPassword() + salt).getBytes());
            if (!newPassword.equals(adUser.getPassword())){
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
            Map<String,Object> result = new HashMap<>();
            adUser.setSalt("");
            adUser.setPassword("");
            result.put("user",adUser);
            result.put("token", AppJwtUtil.getToken(adUser.getId().longValue()));

            return ResponseResult.okResult(result);
        }else {
            //2.不登录
            Map<String,Object> result = new HashMap<>();
            result.put("token", AppJwtUtil.getToken(0l));
            return ResponseResult.okResult(result);
        }
    }

    @Override
    public ResponseResult pageList(AuthDto dto) {

        return userClient.pageList(dto);
    }


    @Override
    public ResponseResult authFail(AuthDto dto) {
        return userClient.authFail(dto);
    }

    @Override
    public ResponseResult authPass(AuthDto dto) {
        return userClient.authPass(dto);
    }

}
